﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fashion_API;

namespace Fashion.BackEnd.ManageAccount
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.UrlReferrer != null)
                    litUrlReferrer.Text = Request.UrlReferrer.ToString();
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            using (DALDataContext d = new DALDataContext())
            {
                Account account =
                    d.Accounts.SingleOrDefault(
                        i =>
                        i.IsActive && i.UserRole.LevelRole <= 2 && i.UserName.Equals(txtUserName.Text) &&
                        i.Password.Equals(txtPassword.Text));
                if (account !=null)
                {
                    A.M.SaveAccountLoginToCookies(account);
                    if (!string.IsNullOrEmpty(litUrlReferrer.Text))
                        A.M.Redirect(litUrlReferrer.Text);
                    else 
                        A.M.Redirect("/BackEnd/Tool/ManagePage/Default.aspx");
                }
                else
                {
                    lblError.Text = "Login failed! Please check it!";
                }
            }
        }
    }
}